package com.ikun.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.ikun.entity.MaintenanceOrders;
import com.ikun.service.MaintenanceOrdersService;
import com.ikun.util.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @ClassName: MaintenanceOrdersController
 * @Author: 吴思伟
 * @CreateTime: 2025-03-26 10:08
 * @Description: 维修订单管理相关
 */
@Controller
@RequestMapping("/maintenance")
@Tag(name = "维修订单管理相关", description = "维修订单管理相关")
public class MaintenanceOrdersController {

    @Autowired
    private MaintenanceOrdersService maintenanceOrdersService;

    @GetMapping("/assignedOrders")
    @ResponseBody
    public Result showAssignedOrders() {
        return Result.success(maintenanceOrdersService.findAssignedOrders());
    }

    @GetMapping("/completedOrders")
    @ResponseBody
    public Result showCompletedOrders() {
        return Result.success(maintenanceOrdersService.findCompleted());
    }

    /**
     * 创建维修工单
     *
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result addMaintenanceOrders(@RequestBody MaintenanceOrders req) {
        return Result.success(maintenanceOrdersService.addMaintenanceOrders(req));
    }

    @PostMapping("/complete")
    @Operation(summary = "标记维修 工单为已完成状态", description = "根据工单ID将工单标记为已完成状态")
    @ResponseBody
    public Result markOrderAsCompleted(String orderId, String staffId) {
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("order_id", orderId);
        requestBody.put("staff_id", staffId);
        return maintenanceOrdersService.markOrderAsCompleted(requestBody);
    }

    @GetMapping("/work-orders")
    @ResponseBody
    public Result getPendingOrders(@RequestParam("staff_id") Integer staffId) {
        return maintenanceOrdersService.getPendingOrders(staffId);
    }

    @GetMapping("/allWorkOrdersByMangerId")
    @ResponseBody
    @Operation(summary = "查询所有工单", description = "根据管家ID查询所有工单")
    public Result showAllWorkOrdersByMangerId(Integer mangerId) {
        return Result.success(maintenanceOrdersService.findAllWorkOrdersByMangerId(mangerId));
    }

    @GetMapping("/allWorkOrders")
    @ResponseBody
    @Operation(summary = "查询所有工单", description = "查询所有工单")
    public Result showAllWorkOrders() {
        return Result.success(maintenanceOrdersService.findAllWorkOrders());
    }

    @PostMapping("/assignStaff")
    @ResponseBody
    @Operation(summary = "分配维修人员", description = "根据工单ID分配维修人员")
    public Result assignStaff(@RequestParam("orderId") String orderId, @RequestParam("staffId") Integer staffId) {
        return Result.success(maintenanceOrdersService.assignStaff(orderId, staffId));
    }
}
